package GUI;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JComboBox;

import Common.Course;
import Common.CourseOfStudent;
import Common.StudentClass;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Vector;

public class AttributeClassToCourse extends JPanel {
	
	private JLabel lblAttributeClassTo=null;
	private JLabel lblSelectCourse=null;
	private JLabel lblSelectTeacher = null;
	private JLabel lblSelectClass = null;
	
	private JButton btnAdd = null;
	//private JButton btnFinish = null;
	private JComboBox cBSelTeacher = null;
	public Login myLoginFrame = null;
	public UserAdmin user=null;
	public AdminMainMenu amn=null;	
	private String StudyUnit = null;
	private String selTeacher = null;
	int hours=0;
	
	private JButton getButtonAdd()
	{
		if(btnAdd==null)
		{
			btnAdd = new JButton("Add");
			btnAdd.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					amn.user.dataAttClassToCourse.setSelTeacher(selTeacher);
										
					String ClassId=amn.user.dataAttClassToCourse.getSelClass();// need to set it to combobox select
					String[] temp=amn.user.dataAttClassToCourse.getSelCourse().split(" ");
					String CourseId=temp[1];
					temp=amn.user.dataAttClassToCourse.getSelTeacher().split(" ");
					String TeacherId=temp[2];
					
					Vector<CourseOfStudent> CoursesStud=new Vector<CourseOfStudent>() ;
					
					Vector<String> PrecoursesOfSelectedCourse=new Vector<String>();
					for(int i=0;i<amn.user.dataAttClassToCourse.CoursesWithPreCourses.size();i++)
					{
						if(amn.user.dataAttClassToCourse.CoursesWithPreCourses.get(i)[0].equalsIgnoreCase(CourseId))
							PrecoursesOfSelectedCourse.add(amn.user.dataAttClassToCourse.CoursesWithPreCourses.get(i)[1]);
							
					}//store the pre courses of selected course
					for(int i=0; i<PrecoursesOfSelectedCourse.size();i++)
					{
						System.out.println("\nPreCourse: "+PrecoursesOfSelectedCourse.get(i));  
					}
					
				
					Vector<String> StudentThatDidTheCourse=new Vector<String>();
					
					for(int i=0;i<amn.user.dataAttClassToCourse.CoursesOfStudOfClass.size();i++)
					{
						if(amn.user.dataAttClassToCourse.CoursesOfStudOfClass.get(i).getClassID().equalsIgnoreCase(ClassId))
						{
							if(amn.user.dataAttClassToCourse.CoursesOfStudOfClass.get(i).getCid().equalsIgnoreCase(CourseId))
								StudentThatDidTheCourse.add(amn.user.dataAttClassToCourse.CoursesOfStudOfClass.get(i).getSid());//Store the students that already take the selected course
							
							for(int j=0;j<PrecoursesOfSelectedCourse.size();j++)
							{
								if(amn.user.dataAttClassToCourse.CoursesOfStudOfClass.get(i).getCid().equalsIgnoreCase(PrecoursesOfSelectedCourse.get(j)))
									CoursesStud.add(amn.user.dataAttClassToCourse.CoursesOfStudOfClass.get(i));
								
							}
						}
								
					}//store the courses (that are precourses of the selected course)of students of the selected class
					for(int i=0;i<amn.user.dataAttClassToCourse.CoursesOfStudOfClass.size();i++)
					{
						System.out.println("\nStudent with PreCourse: "+ amn.user.dataAttClassToCourse.CoursesOfStudOfClass.get(i).getSid());
					}
					
					for(int i=0;i<CoursesStud.size();i++)
					{
						System.out.println("\nPreCourse: "+CoursesStud.get(i).getCid()+ " Student "+CoursesStud.get(i).getSid() );
					}
					
					System.out.println("\nClassId: "+ClassId);
					Vector<StudentClass> StudOfClass=new Vector<StudentClass>();
					for(int i=0;i<amn.user.dataAttClassToCourse.StudentsWithClasses.size();i++)
					{
						if(amn.user.dataAttClassToCourse.StudentsWithClasses.get(i).ClassId==null) i++;
						else
						{
							if(amn.user.dataAttClassToCourse.StudentsWithClasses.get(i).ClassId.equalsIgnoreCase(ClassId))
							StudOfClass.add(amn.user.dataAttClassToCourse.StudentsWithClasses.get(i));
						}
					}//store the all students of the class
					
					for(int i=0;i<StudOfClass.size();i++)
					{
						System.out.println("\nStudent of the class: "+StudOfClass.get(i).Sid );
					}
					
				
					Vector<Integer[]>CountOfPreCoursesToStud=new Vector<Integer[]>();//[0]-student id,[1]-count of precourses that the sud have to selected course
					for(int i=0;i<StudOfClass.size();i++)
					{
						int count=0;
						Integer[] array=new Integer[2];
						array[0]=Integer.parseInt(StudOfClass.get(i).Sid);
						array[1]=0;
						for(int j=0;j<CoursesStud.size();j++)
						{
							if(CoursesStud.get(j).getSid().equals(StudOfClass.get(i).Sid))
								count++;
						}
						array[1]=count;
						
						CountOfPreCoursesToStud.add(array);
					}
					Vector<StudentClass> OKStudents=new Vector<StudentClass>();//they will take the course
					Vector<StudentClass> ExStudents=new Vector<StudentClass>();
					for(int i=0;i<CountOfPreCoursesToStud.size();i++)
					{
						System.out.println("\n Count of Student: "+CountOfPreCoursesToStud.get(i)[0] + " count: "+CountOfPreCoursesToStud.get(i)[1]  );
						if(CountOfPreCoursesToStud.get(i)[1]==PrecoursesOfSelectedCourse.size())
						{
							for(int j=0;j<StudOfClass.size();j++)
							{
								if(StudOfClass.get(j).Sid.equalsIgnoreCase(CountOfPreCoursesToStud.get(i)[0].toString()))
									OKStudents.add(StudOfClass.get(j));
								else ExStudents.add(StudOfClass.get(j));
									
							}
						}
					}
					
					
					
					int numOfPreCOfSelectedCourse=PrecoursesOfSelectedCourse.size();
					
					
					
					//System.out.println("\n Students That did the course:" + StudentThatDidTheCourse.get(0));
					for(int i=0;i<OKStudents.size();i++)
					{
						for(int j=0;j<StudentThatDidTheCourse.size();j++)
						{
							if(OKStudents.get(i).Sid.equalsIgnoreCase(StudentThatDidTheCourse.get(j)))
							{
								ExStudents.add(OKStudents.remove(i));
								
							}
						}
						
					}
					
					
					
					if (!ExStudents.isEmpty())
						{System.out.println("\n ExStudents:" + ExStudents.firstElement().Sid);
						
						}
					
					// in case of course with no pre-courses or all students get the pre-courses ExStudents is empty
					//need to prompt this list to the user
					if(!OKStudents.isEmpty())
						{
						String toQuery="AttributeStudentsToCourse";//,Cid,Year,Semester,Cname
						toQuery=toQuery.concat(" "+CourseId);
						//toQuery.concat(" "+Year);
						//toQuery.concat(" "+Semester);
						//toQuery.concat(Cname);
						
						
						for(int i=0; i<OKStudents.size();i++)
						{
							toQuery=toQuery.concat(" "+OKStudents.get(i).Sid);
						}
						toQuery=toQuery.concat(" "+TeacherId+" "+ClassId);
						System.out.println("\n Query: " +toQuery); 
						amn.user.dataAttClassToCourse.ApprovalToAtt=false;
						amn.myLoginFrame.client.handleMessageFromClientUI(toQuery);
						while(!amn.user.dataAttClassToCourse.ApprovalToAtt);
						
						if(!ExStudents.isEmpty())
						{
							String msg="The students that can't be attributed to the course are:\n";
							for(int i=0;i<ExStudents.size();i++)
							{
								msg=msg.concat(" "+ExStudents.get(i).FirstName+" "+ExStudents.get(i).LastName +"\n");
							}
							msg=msg.concat("Other students were been attributed successfuly");
							JOptionPane.showMessageDialog(amn, msg);
						}
						else
							{
							String msg= "The class was been attributed to the course successfuly";
							JOptionPane.showMessageDialog(amn, msg);
							}
						
						
						}
					else 
						{
						
						JOptionPane.showMessageDialog(amn, "Class can't be attributed to chosen course because there is no student that completed \n the requierments  or didn't completed this course already  ");
						
						}
				//TODO - set this course to the classescourses table and for each student to student courses
					
					
					//TODO what happends when a parent clicks "mntmAttributeClassTo" 
					
				}
			});
			btnAdd.setBounds(410, 239, 91, 23);
			
		}
		//TODO before addition we have to check that all of the students in the class finished all the required courses if not popup window with the names of the students that 
		//couldnt be registered
		return btnAdd;
	}
	
	
	/*private JButton getButtonFinish()
	{
		if(btnFinish==null)
		{
			btnFinish = new JButton("Finish");
			btnFinish.setBounds(524, 239, 91, 23);
			
		}
		return btnFinish;
	}*/
	
	private JComboBox getSelTeacherCB(AdminMainMenu amn)
	{
		Vector<String> data = new Vector<String>();
		int i;
		
		for(i=0;i<amn.user.dataAttClassToCourse.RawCourses.size();i++)
		{
			if(amn.user.dataAttClassToCourse.getSelCourse().endsWith(amn.user.dataAttClassToCourse.RawCourses.get(i).Cid))
			{
				 StudyUnit=amn.user.dataAttClassToCourse.RawCourses.get(i).StudUnit;
				 hours = amn.user.dataAttClassToCourse.RawCourses.get(i).StudHours;
			}
				
		}
		
		for(i=0;i<amn.user.dataAttClassToCourse.Teachers.size();i++)
		{
			if(amn.user.dataAttClassToCourse.Teachers.get(i).StudUnit.equalsIgnoreCase(StudyUnit)&&
					(amn.user.dataAttClassToCourse.Teachers.get(i).FreeHours>=hours))
			{
				data.add(amn.user.dataAttClassToCourse.Teachers.get(i).FirstName+" "+amn.user.dataAttClassToCourse.Teachers.get(i).LastName+" "+amn.user.dataAttClassToCourse.Teachers.get(i).tid);
			}
		}
		
		
		
		
		if(cBSelTeacher==null)
		{
			cBSelTeacher = new JComboBox(data);
			cBSelTeacher.addActionListener((new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					selTeacher=cBSelTeacher.getSelectedItem().toString();
			 		System.out.println(selTeacher);
					
					
				}
			}));
			cBSelTeacher.setBounds(124, 131, 125, 22);
			
		}
		
		return cBSelTeacher;
			
	}
	
	private void initialize(AdminMainMenu amn)
	{
		
		setLayout(null);
		
		lblAttributeClassTo = new JLabel("Attribute class to course");
		lblAttributeClassTo.setBounds(269, 11, 117, 14);
		
		lblSelectTeacher = new JLabel("Select teacher:");
		lblSelectTeacher.setBounds(31, 135, 73, 14);
		this.add(lblAttributeClassTo);
		this.add(lblSelectTeacher);

		
		this.add(getButtonAdd());
		
		 
		//this.add(getButtonFinish());
		
		
		this.add(getSelTeacherCB(amn));
		
	}
	
	
	
	
	public AttributeClassToCourse(AdminMainMenu amn) {
		super();
		initialize(amn);
		
	}
	
	public void setCoursesOfStudOfClass(Vector<CourseOfStudent> c)
	{
		this.amn.user.dataAttClassToCourse.CoursesOfStudOfClass=c;
	}
}
